package com.sinothk.hussars.controller;


import com.sinothk.hussars.base.BaseController;
import com.sinothk.hussars.base.entity.PageReq;
import com.sinothk.hussars.entity.UserEntity;
import com.sinothk.hussars.entity.UserVo;
import com.sinothk.hussars.service.UserService;
import com.sinothk.hussars.utils.XUtils;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 机构用户表 前端控制器
 * </p>
 *
 * @author liangyt
 * @since 2021-01-03
 */
@Api(tags = "机构_采集_受检人员")
@RestController
@RequestMapping("/user")
@ResponseResult()
public class UserController extends BaseController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "新增：受检人员")
    @PostMapping("/save")
    public UserEntity save(@RequestBody UserVo userVo) {
        // 参数验证
        if (StringUtil.isEmpty(userVo.getIdNum())) throw new BizException("身份证不能为空");
        return userService.add(userVo);
    }

    @ApiOperation(value = "更新：受检人员")
    @PostMapping("/update")
    public UserEntity update(@RequestBody UserVo userVo) {
        // 参数验证
        if (StringUtil.isEmpty(userVo.getId())) throw new BizException("人员Id不能为空");

        UserEntity user = XUtils.classUtil().copyByMethod(userVo, UserVo.class, UserEntity.class);
        user.setUpdateDate(new Date());
        userService.updateById(user);
        return userService.getById(userVo.getId());
    }

    @ApiOperation(value = "查询：受检人员列表")
    @PostMapping("/usersPage")
    public PageData usersPage(@RequestBody PageReq<UserEntity> pageVo) {
        // 参数验证
        if (pageVo.getPageSize() == 0) pageVo.setPageSize(20);
        return userService.usersPage(pageVo);
    }

    @ApiOperation(value = "查询：受检人员详情")
    @GetMapping("/{id}")
    public UserEntity detail(@PathVariable String id) {
        // 参数验证
        if (StringUtil.isEmpty(id)) throw new BizException("人员Id不能为空");
        return userService.getById(id);
    }
}

